package cn.eleven.controller;

import cn.eleven.entity.ForumPost;
import cn.eleven.entity.User;
import cn.eleven.mapper.ForumPostMapper;
import cn.eleven.mapper.UserMapper;
import cn.eleven.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin("*")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ForumPostMapper forumPostMapper;

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userMapper.getUserById(id);
        return Result.success(user);
    }

//    @DeleteMapping("/forum_post/{postId}")
//    public Result deleteForumPost(@PathVariable Integer postId) {
//        boolean success = forumPostMapper.deleteById(postId) > 0;
//        return success ? Result.success(null) : Result.error("Delete failed");
//    }

    @PutMapping("/forum_post")
    public Result updateForumPost(@RequestBody ForumPost forumPost) {
        boolean success = forumPostMapper.updateById(forumPost) > 0;
        return success ? Result.success(forumPost) : Result.error("Update failed");
    }
    @GetMapping("/by_email/{email}")
    public Result getUserByEmail(@PathVariable String email) {
        User user = userMapper.selectByEmail(email);
        if (user != null) {
            return Result.success(user);
        } else {
            return Result.error("User not found");
        }
    }
    @PutMapping("/{id}")
    public Result updateForumPost(@PathVariable Integer id, @RequestBody ForumPost forumPost) {
        forumPost.setId(id); // Ensure the ID in the request body matches the path variable
        boolean success = forumPostMapper.updateById(forumPost) > 0;
        return success ? Result.success(forumPost) : Result.error("Update failed");
    }
}